package cn.jiedh.app.meiweidk.Utils.UI;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.NetworkUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;

/**
 * 处理断网最外层布局 直接替换掉最外层linearlayout
 */

public class StateLayout extends LinearLayout {

    private LayoutInflater inflater;
    private Context context;

    public View getNetView() {
        return netView;
    }

    public void setNetView(View netView) {
        this.netView = netView;
    }

    private View netView;
    private TextView nn_text, nn_btn;
    private ImageView nn_pic;
    private NoNetView monNetClick;


    private View progressView;


    public StateLayout(Context context) {
        this(context, null);
    }


    public StateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public StateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        parseAttrs(context, attrs);

        if (NetworkUtils.isConnect(context)) {
            if (netView != null) {
                netView.setVisibility(View.GONE);
            }
//            initprogressView();
//            progressView.setVisibility(View.GONE);
        }


    }

    private void parseAttrs(final Context context, AttributeSet attrs) {
        inflater = LayoutInflater.from(context);
        initnetView();

    }

    protected void initnetView() {
        netView = inflater.inflate(R.layout.layout_nonetwork, this, false);
        netView.findViewById(R.id.refresh).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (NetworkUtils.isConnect(context)) {
                    if (monNetClick!=null) {
                        monNetClick.setOnclck(view);
                        netView.setVisibility(View.GONE);
                    }
                } else {
                    T.showShortToast("网络开了个小差");
                    netView.setVisibility(View.VISIBLE);
                }


            }
        });
        addView(netView);
    }

    /**
     * 判断网络连接 隐藏网络断开布局
     */
    public void netviewGone(){
        if (NetworkUtils.isConnect(context)) {
            netView.setVisibility(View.GONE);
        } else {
            netView.setVisibility(View.VISIBLE);
        }
    }

    public void setOnNoClick(final NoNetView onNetClick) {
        this.monNetClick=onNetClick;
    }


    public void showNetWorkView(int show) {
        if (netView != null)
        netView.setVisibility(show);
    }
    public void showProgressView(int show) {
        if (progressView != null)
            progressView.setVisibility(show);
    }


    public interface NoNetView{
        void setOnclck(View view);
    }


    /**
     * addView
     */

    @Override
    public void addView(View child) {
        super.addView(child);
    }

    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
    }

    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
    }

    @Override
    protected boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params) {
        return super.addViewInLayout(child, index, params);
    }

    @Override
    protected boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout) {
        return super.addViewInLayout(child, index, params, preventRequestLayout);
    }

    /**
     * removeView
     */

    @Override
    public void removeView(View view) {
        super.removeView(view);
    }

}